import apis from '@h/apis'
export default {
    namespaced: true,
    state: {
        searchVal: {
            type: 'name',
            value: '',
        },
        //分类列表
        insuresArr: [],
        //修改数据
        updateInsures: {
            //产品名称
            name: "",
            brief: '',
            //产品封面
            coverMap: "",
            //产品详情
            coverDetails: "",
            //缴费类型
            payType: "",
            //保单形式
            policy: [],
            //价格
            price: "",
            //是否可续保
            renew: false,
            //适用人群
            suitableFor: "",
            //所属分类
            typesId: "",
        },
        //分页条件
        total: 1,
        currentPage: 1,
        pageSize: 3,
    },
    mutations: {
        changeSearchVal(state, newData) {
            state.searchVal = newData;
        },
        changeInsuresArr(state, newData) {
            state.insuresArr = newData;
        },
        changeUpdateInsures(state, newData) {
            state.updateInsures = newData;
        },
        changeTotal(state, newData) {
            state.total = newData;
        },
        changeCurrentPage(state, newData) {
            state.currentPage = newData;
        },
        changePageSize(state, newData) {
            state.pageSize = newData;
        },
    },
    actions: {
        async getInsures(context) {
            const { searchVal, currentPage, pageSize, insuresArr, total } = context.state;
            const params = {
                ...searchVal,
                currentPage,
                pageSize
            };
            const { data } = await apis.insures.getInsures(params);
            if (data.status) {
                context.commit('changeTotal', data.data.total);
                context.commit('changeInsuresArr', data.data.insures);
            }
        },
        async getInsureById(context, params) {
            const { data } = await apis.insures.getInsureById(params);
            if (data.status) {
                context.commit('changeUpdateInsures', data.data[0]);
            }
        },
        async addInsure(context, insure) {
            return await apis.insures.addInsure(insure);
        },
        async updateInsureById(context, insure) {
            return await apis.insures.updateInsureById(insure);
        },
        async delInsureById(context, id) {
            return await apis.insures.delInsureById(id);
        },
    }
}